package com.tedu.openmedical.medicalsystem.ICD10.service.impl;

import com.tedu.openmedical.medicalsystem.ICD10.dto.ICDQuery;
import com.tedu.openmedical.medicalsystem.ICD10.dto.ICDSaveParam;
import com.tedu.openmedical.medicalsystem.ICD10.entity.ICD;
import com.tedu.openmedical.medicalsystem.ICD10.service.ICDService;
import com.tedu.openmedical.medicalsystem.ICD10.vo.ICDVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tedu.openmedical.medicalsystem.ICD10.mapper.ICDMapper;

import java.util.Date;
import java.util.List;

@Service
public class ICDServiceImpl implements ICDService {

    @Autowired
    private ICDMapper ICDMapper;

    @Override
    public List<ICDVO> select(ICDQuery query) {
        if (query.getTimes() == null) {
            return ICDMapper.select(query);
        }
        query.setStartTime(query.getTimes()[0]);
        query.setEndTime(query.getTimes()[1]);
        return ICDMapper.select(query);
    }

    @Override
    public void save(ICDSaveParam ICDSaveParam) {
        ICD ICD = new ICD();
        BeanUtils.copyProperties(ICDSaveParam, ICD);
        if (ICDSaveParam.getId() == null){
            ICD.setUpdateTime(new Date());
            ICD.setCreateTime(new Date());
            ICDMapper.insert(ICD);
        } else {
            ICD.setUpdateTime(new Date());
            ICD.setUpdateBy(ICDSaveParam.getCreateBy());
            ICDMapper.update(ICD);
        }
    }

    @Override
    public void delete(Long id) {
        ICDMapper.delete(id);
    }
}
